package com.chenfan.process.context;

import com.chenfan.process.event.callback.TriggerCallbackEvent;
import com.chenfan.process.event.callback.TriggerCallbackEventData;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author K
 * @date 2022/7/12 5:12 PM
 */
public class ApprovalEventContext {
    private static final InheritableThreadLocal<TriggerCallbackEvent> EVENT_CONTEXT = new InheritableThreadLocal<>();

    private ApprovalEventContext() {}

    public static void addEvent(TriggerCallbackEventData eventData) {
        TriggerCallbackEvent event = EVENT_CONTEXT.get();
        if (Objects.isNull(event)) {
            event = new TriggerCallbackEvent(new ArrayList<>());
        }
        List<TriggerCallbackEventData> callbackEvents = event.getTriggerCallbackEventDataList();
        callbackEvents.add(eventData);
        EVENT_CONTEXT.set(event);
    }

    public static TriggerCallbackEvent getEvent() {
        return EVENT_CONTEXT.get();
    }


    public static void clear() {
        EVENT_CONTEXT.remove();
    }
}
